/**
 * @description template router
 */

const router = require('koa-router')()
const { templateDataSchema } = require('../validator/template')
const genValidator = require('../middleware/genValidator')

const {
  getTemplateList,
  updateIsPublic,
  updateIsHot,
  updateOrderIndex,
  getCount,
  getMonthlyCount
} = require('../controller/template')

// 路由前缀
router.prefix('/api/template')

// 获取模板列表
router.get('/', async ctx => {
  const { keyword = '', pageIndex, pageSize } = ctx.query
  const res = await getTemplateList(decodeURIComponent(keyword), pageIndex, pageSize)
  ctx.body = res
})

// 设置 isPublic
router.patch('/isPublic', genValidator(templateDataSchema), async ctx => {
  const { ids, isPublic } = ctx.request.body
  const res = await updateIsPublic(ids, isPublic)
  ctx.body = res
})

// 设置 isHot
router.patch('/isHot', genValidator(templateDataSchema), async ctx => {
  const { ids, isHot } = ctx.request.body
  const res = await updateIsHot(ids, isHot)
  ctx.body = res
})

// 设置 orderIndex
router.patch('/orderIndex', genValidator(templateDataSchema), async ctx => {
  const { ids, orderIndex } = ctx.request.body
  const res = await updateOrderIndex(ids, orderIndex)
  ctx.body = res
})

// 获取总数
router.get('/getCount', async ctx => {
  const res = await getCount()
  ctx.body = res
})

// 按月统计
router.get('/getMonthlyCount', async ctx => {
  const res = await getMonthlyCount()
  ctx.body = res
})

module.exports = router
